﻿var options = { items_per_page: 20, link_to: "javascript:;", current_page: 0, callback: pageChanged };
var coursename;
var serialnumber;
var catalogid;
var includesub;
var coursetype;
var maxentries = 0;
$(function() {
    createSearchPanel();
    getValue();
    BindSearchSwitch();
    $("#txt_catalog").catalogs({ dataurl: rootpath + "api/course.ashx", parmsdata: { type: "getcatalogs", pid: 0} });
    searchCourses();
    BindCheckBoxEvent($("#btn_passed"));
    $("#btn_search").click(function() {
        getValue();
        searchCourses();
    });

    bindMultPassedEvent();
});
function getValue() {
    coursename = $("#txt_coursename").val();
    serialnumber = $("#txt_serialnumber").val();
    catalogid = $("#txt_catalog").attr("rel");
    coursetype = $("#ddl_coursetype option:selected").val();

    if ($("#cb_include").attr("checked")) {
        includesub = 1;
    }
    else {
        includesub = 0;
    }
}
function searchCourses() {
    $.ajax({
        type: "GET",
        url: rootpath + "api/course.ashx",
        data: { type: 'coursenum', coursename: coursename, ctype: coursetype, serialnumber: serialnumber, catalogid: catalogid, includesub: includesub, status: 0, isdelete: 0, r: Math.random() },
        dataType: "html",
        success: function(data) {
            maxentries = data;
            $("#cb_All").attr("checked", "");
            if (data == 0) {
                $(".DG tbody").hide();
                $(".DG table").next().hide();
                $(".warning").show();
                formReady();
                return;
            }
            var pages = maxentries / options.items_per_page;

            if (pages <= options.current_page) {
                options.current_page = pages - 1;
            }

            $("#pager").pagination(data, options);
            $(".DG tbody").show();
            $(".DG table").next().show();
            $(".warning").hide();
        }
    });
}
function pageChanged(index, jq) {
    options.current_page = index;

    $.ajax({
        type: "GET",
        url: rootpath + "api/course.ashx",
        data: { type: 'searchcourse', coursename: coursename, ctype: coursetype, serialnumber: serialnumber, catalogid: catalogid, includesub: includesub, status: 0, isdelete: 0, pageindex: index + 1, pagesize: options.items_per_page, r: Math.random() },
        dataType: "json",
        success: function(data) {
            $(".DG tbody").empty();
            $.each(data, function(i, item) {

                $(".DG tbody")
                .append("<tr><td><input type='checkbox' value='" + item.courseid + "' /></td>\
                            <td>" + item.row + "</td>\
                            <td>" + item.coursename + "</td>\
                            <td>" + item.serialnumber + "</td>\
                            <td>" + item.catalogname + "</td>\
                            <td>" + item.coursetypename + "</td>\
                            <td>" + item.playlength + "</td>\
                            <td>\
                                <input type='button' class='viewButton' title='查看课程详细' /> \
                                <input type='button' class='deleteButton' rel='" + item.courseid + "' relname='" + item.coursename + "' title='删除课程'/>\
                            </td>\
                        </tr>");

                $(".viewButton:last").click(function() {

                    var location = 'course/preview.aspx?cid=' + item.courseid;                
                    openDialog(location, "课程详细", 900, 560);
                    return false;
                });
                BindCheckBox($("#btn_passed"), $(":checkbox:last"));
            });

            bindDeleteEvent();
            $(".DG tbody tr").hover(function() { $(this).addClass("tdmover"); }, function() { $(this).removeClass("tdmover"); });
            formReady();
        }
    });
}
function bindMultPassedEvent() {
    $("#btn_passed").click(function() {
        confirmbox("确定将所选课程都通过审核吗？");

        $(window.parent.document).find("#btn_sure").click(function() {
            closebox();

            var cids = "";
            $("input:checked").not("#cb_All,#cb_include").each(function() {
                cids += $(this).val() + ",";
            });

            $.ajax({
                type: "POST",
                url: rootpath + "course/audit/audit.ashx",
                data: { type: 'coursepassed', cids: cids, r: Math.random() },
                dataType: "string",
                success: function(data) {
                    if (data == 1) {
                        searchCourses();
                        facebox("操作已完成！");
                    }
                }
            });
        });
    });
}
function bindDeleteEvent() {
    $(".deleteButton").click(function() {
        var cid = $(this).attr("rel");
        var cname = $(this).attr("relname");

        closeDialog();

        confirmbox("<div>确定要删除课程 <strong>" + cname + "</strong> 吗？</div>");

        $(window.parent.document).find("#btn_sure").click(function() {

            $.ajax({
                type: "POST",
                url: rootpath + "course/create/create.ashx",
                data: { type: 'deletecourse', uids: cid, r: Math.random() },
                dataType: "string",
                success: function(data) {
                    if (data == 1) {
                        closebox();
                        searchCourses();
                    }
                }
            });
        });
    });
}
